import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class Main822 {
    static AtomicInteger nums = new AtomicInteger(0);
    public static void main(String[] args) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0;i<50;i++){
                    nums.addAndGet(1);
                    System.out.println("第1个线程"+nums);
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0;i<50;i++){
                    nums.addAndGet(1);
                    System.out.println("第2个线程"+nums);
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0;i<50;i++){
                    nums.addAndGet(1);
                    System.out.println("第3个线程"+nums);
                }
            }
        }).start();

    }
    static class M282{
        static int num = 0;
        public static void main(String[] args) {
            Semaphore semaphore = new Semaphore(0);
            new Thread(() -> {
                try {
                    // 等待1s
                    TimeUnit.MILLISECONDS.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 操作共享变量
                num++;
                semaphore.release(2);  // 放入2个许可证
            }).start();

            new Thread(() -> {
                try {
                    // 这里会阻塞，直到有一个许可证为止
                    semaphore.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程2获取到num=" + num);
            }).start();

            new Thread(() -> {
                try {
                    // 这里会阻塞，直到有一个许可证为止
                    semaphore.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程3获取到num=" + num);
            }).start();
        }
    }
}
